package com.winning.sjzc.zyml.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.winning.sjzc.common.entity.po.TreeEntity;
import com.winning.sjzc.common.utils.ConverterUtils;
import com.winning.sjzc.common.utils.PageBean;
import com.winning.sjzc.common.utils.PaginationResponseEntity;
import com.winning.sjzc.common.utils.ResponseEntity;
import com.winning.sjzc.zyml.entity.PsmsSjjflEntity;
import com.winning.sjzc.zyml.entity.PsmsSjyxxEntity;
import com.winning.sjzc.zyml.entity.vo.SjjywxxEntity;
import com.winning.sjzc.zyml.service.PsmsSjjflService;
import com.winning.sjzc.zyml.service.PsmsSjjxxService;
import com.winning.sjzc.zyml.service.PsmsSjyxxService;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.winning.sjzc.zyml.utils.SjjflTreeUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

/**
 * description: 业务资源目录
 *
 * @author yanxf@winning.com.cn
 * @date 2019/08/14 14:27
 */
@Slf4j
@RequestMapping("/ywzyml")
@RestController
public class YwzymlContrller {

  @Autowired
  private PsmsSjjxxService psmsSjjxxService;

  @Autowired
  private PsmsSjyxxService psmsSjyxxService;

  @Autowired
  private PsmsSjjflService psmsSjjflService;

  @RequestMapping("/list")
  public ModelAndView ywzyml(String sjjfldm, Model model) {

//    if (StringUtils.isEmpty(sjjfldm)) {
//      sjjfldm = "010101";
//    }
//    List<PsmsSjjflEntity> all1 = psmsSjjflService.findBySjjfldm("01");
//    List<PsmsSjjflEntity> all2 = psmsSjjflService.findBySjjfldm("02");
//    List<PsmsSjjflEntity> all3 = psmsSjjflService.findBySjjfldm("03");
//
//    TreeEntity<PsmsSjjflEntity> treeEntity1 = getTreeEntity(all1);
//    if (CollectionUtils.isNotEmpty(all3)) {
//      TreeEntity<PsmsSjjflEntity> treeEntity3 = getTreeEntity(all3);
//      model.addAttribute("tree3", treeEntity3);
//    }
//
//    if (CollectionUtils.isNotEmpty(all3)) {
//      TreeEntity<PsmsSjjflEntity> treeEntity2 = getTreeEntity(all2);
//      model.addAttribute("tree2", treeEntity2);
//    }
//    model.addAttribute("tree", treeEntity1);




//    PsmsSjjflEntity psmsSjjflEntity = psmsSjjflService.selectById(sjjfldm);
//
//    model.addAttribute("flsm", psmsSjjflEntity.getFlsm());
//    model.addAttribute("sjjfldm", sjjfldm);
//    List<SjjywxxEntity> sjjxxEntities = psmsSjjxxService.findByFldm(sjjfldm);
//    Map<String, Object> sumMap = psmsSjjxxService.querySjjNum(sjjfldm);
//    model.addAttribute("sjjxxEntities", sjjxxEntities);
//    model.addAttribute("sumMap", sumMap);
    ModelAndView mv = new ModelAndView("ywzyml");
    return mv;
  }

  /**
   * 表容量 记录数  查询
   * @param sjjfldm
   * @return
   */
  @GetMapping("/queryData")
  public ResponseEntity queryData(String sjjfldm){
    Map<String,Object> sumMap = psmsSjjxxService.querySjjNum(sjjfldm);
    PsmsSjjflEntity psmsSjjflEntity = psmsSjjflService.selectById(sjjfldm);
    sumMap.put("flsm",psmsSjjflEntity.getFlsm());
    return ResponseEntity.ok(sumMap);
  }


  /**
   * 分页查询
   * @param paraMaps
   * @return
   */
  @PostMapping("/fragmentList")
  @ResponseBody
  public ResponseEntity fragmentList(@RequestBody Map paraMaps) {
    PageBean page = (PageBean) ConverterUtils.map2ObjectWithoutNullValue((Map<String, String>) paraMaps.get("page"),PageBean.class);
    Page<SjjywxxEntity> sjjywxxPage = new Page<>(page.getPageNo(),page.getPageSize());
    IPage<SjjywxxEntity> sjjywxxList = null;
    try {
      sjjywxxList = psmsSjyxxService.queryPageSjjywxx(sjjywxxPage,paraMaps);
      page.setTotalRecord((int) sjjywxxList.getTotal());
    }catch(Exception e){
      log.error("",e);
      return PaginationResponseEntity.error(500,"查询失败");
    }
    return PaginationResponseEntity.ok(page).put("rows",sjjywxxList.getRecords());
  }

//  private TreeEntity<PsmsSjjflEntity> getTreeEntity(List<PsmsSjjflEntity> all) {
//    TreeEntity<PsmsSjjflEntity> treeEntity = new TreeEntity<>();
//    Map<String, List<PsmsSjjflEntity>> collect = all.stream()
//        .collect(Collectors.groupingBy(PsmsSjjflEntity::getFljb));
//
//    PsmsSjjflEntity root = collect.get("1").get(0);
//    treeEntity.setModel(root);
//
//    List<PsmsSjjflEntity> ejflEntities = collect.get("2");
//    List<PsmsSjjflEntity> sjflEntities = collect.get("3");
//
//    Map<String, List<PsmsSjjflEntity>> sjflMap = sjflEntities.stream()
//        .collect(Collectors.groupingBy(PsmsSjjflEntity::getEjlbdm));
//
//    List<TreeEntity<PsmsSjjflEntity>> ejflTrees = getListTreeEntites(ejflEntities);
//    ejflTrees.forEach(psmsSjjfl -> {
//      String sjjfldm = psmsSjjfl.getModel().getSjjfldm();
//      List<PsmsSjjflEntity> psmsSjjflEntities = sjflMap.get(sjjfldm);
//      List<TreeEntity<PsmsSjjflEntity>> sjflTrees = getListTreeEntites(psmsSjjflEntities);
//      psmsSjjfl.setChildren(sjflTrees);
//    });
//    treeEntity.setChildren(ejflTrees);
//    return treeEntity;
//  }

  @GetMapping("/toDetail")
  public ModelAndView toDetail(String sjkbm,ModelMap model){
    model.addAttribute("sjkbm",sjkbm);
    return new ModelAndView("ywzyml-detail","sjyxx",model);
  }


  @PostMapping("/detail")
  @ResponseBody
  public ResponseEntity detail(@RequestBody Map paraMaps) {
    PageBean page = (PageBean) ConverterUtils.map2ObjectWithoutNullValue((Map<String, String>) paraMaps.get("page"),PageBean.class);
    Page<PsmsSjyxxEntity> sjywxxPage = new Page<>(page.getPageNo(),page.getPageSize());
    IPage<PsmsSjyxxEntity> sjyxxList = null;
    try {
      sjyxxList = psmsSjyxxService.querySjyxxPage(sjywxxPage,paraMaps);
      page.setTotalRecord((int) sjyxxList.getTotal());
    }catch(Exception e){
      log.error("",e);
      return PaginationResponseEntity.error(500,"查询失败");
    }
    return PaginationResponseEntity.ok(page).put("rows",sjyxxList.getRecords());
  }

 /* @RequestMapping("/detail")
  public ModelAndView detail(String sjkbm, Model model) {
    List<PsmsSjyxxEntity> sjyxxEntities = psmsSjyxxService.queryList(sjkbm);
    model.addAttribute("sjyxxEntities", sjyxxEntities);
    ModelAndView mv = new ModelAndView("ywzyml-detail");
    return mv;
  }*/

  @RequestMapping("/fragmentDetail")
  public ModelAndView fragmentDetail(String sjkbm, Model model) {
    List<PsmsSjyxxEntity> sjyxxEntities = psmsSjyxxService.queryList(sjkbm);
    model.addAttribute("sjyxxEntities", sjyxxEntities);
    ModelAndView mv = new ModelAndView("ywzyml-detail::fragmentDetail");
    return mv;
  }


//  List<TreeEntity<PsmsSjjflEntity>>  getListTreeEntites(List<PsmsSjjflEntity> list) {
//    List<TreeEntity<PsmsSjjflEntity>> trees = new ArrayList<>();
//    if (CollectionUtils.isEmpty(list)) {
//      return trees;
//    }
//    list.forEach(entity -> {
//      TreeEntity<PsmsSjjflEntity> treeEntity = new TreeEntity<>();
//      treeEntity.setModel(entity);
//      trees.add(treeEntity);
//    });
//    return trees;
//  }

  /**
   * 初始化树
   * @param paraMaps
   * @return
   */
  @PostMapping("/querySjjflList")
  @ResponseBody
  public ResponseEntity querySjjflList(@RequestBody Map paraMaps){
    String fldm = paraMaps.get("fldm")+"";
    //查询记录状态为0的数据集分类
    List<PsmsSjjflEntity> list = psmsSjjflService.findSjjfl();
    SjjflTreeUtil sjjflTreeUtil = new SjjflTreeUtil();
    List<Object> flTreeList = sjjflTreeUtil.sjjflTreeList(list, fldm);
    return ResponseEntity.ok().put("sjjflTreeList", flTreeList);
  }


}
